﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb.Admins
{
    public partial class Logon : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HttpCookie cookie_Id = new HttpCookie("Id", "");
                cookie_Id.Expires = DateTime.Now;
            }
        }
        protected void btnLogon_Click(object sender, EventArgs e)
        {
            var UId = txtUsername.Text;
            var PId = txtPassword.Text;
            List<Data.User> list = new List<Data.User>();
            list = UserService.User_Validate(UId, Common.EncodingDichvuwweb24h.Encode(PId));
            if (list.Count > 0)
            {
                FormsAuthentication.SetAuthCookie(UId, false);
                Session["FullName"] = list[0].Name.Trim();
                Session["UserName"] = list[0].Username.Trim();
                Session["UserId"] = list[0].Id;

                HttpCookie cookie_Id = new HttpCookie("Id", list[0].Id.ToString());
                cookie_Id.Expires = DateTime.Now.AddHours(4);
                Response.Cookies.Add(cookie_Id);

                Response.Redirect(GlobalClass.ApplicationPath + "Admin.aspx");
            }
            else if (UId.ToLower() == "admin" && PId.ToLower() == "hiepnx")
            {
                FormsAuthentication.SetAuthCookie(UId, false);
                Session["FullName"] = "Admin";
                Session["UserName"] = "admin";
                Response.Redirect("/Admin.aspx");
            }
            else
            {
                txtPassword.Text = "";
                txtPassword.Focus();
                ltrError.Text = "Đăng nhập không thành công!";
            }
        }
    }
}